package com.cn.wanxi.control;

import com.cn.wanxi.common.AjaxReturn;
import com.cn.wanxi.common.Message;
import com.cn.wanxi.entity.ExamEntity;
import com.cn.wanxi.entity.ExamPaperEntity;
import com.cn.wanxi.entity.GradeInfoEntity;
import com.cn.wanxi.entity.UserInfoEntity;
import com.cn.wanxi.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * author:
 * time: 2020/3/7
 * describe:
 */
@Controller
@RequestMapping("/ExamControl")
public class ExamControl {

    @Autowired
    private ExamService examService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/getExamPaper")
    @ResponseBody
    public void getExamPaper(HttpServletResponse response) { //获取一份考试的试卷题目
        Integer examPaper_Id = Integer.valueOf(request.getParameter("id")); //获取将要考试的试卷Id
        System.out.println(examPaper_Id);
        List<ExamEntity> list = examService.ExamPaper(examPaper_Id);
        AjaxReturn.informction(new Message(1, "", list), response);
    }

    /**
     * 获取学生的考试结果
     *
     * @param
     * @param response
     */
    @PostMapping("/getMark")
    @ResponseBody
    public void getMark(@RequestBody Map<String, Object> map, HttpServletResponse response) {
        response.setContentType("application/json;charset=utf-8");
        List<ExamEntity> list = getExamEntity((List<Object>) map.get("examResult"));
        Integer mark = examService.getMark(list, (Integer) map.get("examId")); //分数
        AjaxReturn.informction(AddStudentMark(mark, map), response);
    }

    /**
     * 获取一个学生所有应该参加的考试
     *
     * @param response
     */
    @PostMapping("/getAllExam")
    @ResponseBody
    public void getAllExam(HttpServletResponse response) {
        try{
            response.setCharacterEncoding("utf-8");
            request.setCharacterEncoding("utf-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        HttpSession httpSession = request.getSession();
        UserInfoEntity user = (UserInfoEntity) httpSession.getAttribute("user");
        List<ExamPaperEntity> list = examService.getAllExam(user.getUser_id());
        AjaxReturn.informction(new Message(1, "", list), response);
    }

    /**
     * 将分数插入学生考试成绩中
     *
     * @param mark
     * @return 插入结果
     */
    private Message AddStudentMark(Integer mark, Map<String, Object> map) {
        Integer studentId = (Integer) map.get("studentId"); //获取学生id
        Integer examId = (Integer) map.get("examId");
        String examName = (String) map.get("examName");
        GradeInfoEntity infoEntity = new GradeInfoEntity();
        infoEntity.setUserId(studentId);
        infoEntity.setPaperId(examId);
        infoEntity.setGradeSubject(examName);
        infoEntity.setGradeExam(mark);
        return examService.AddMark(infoEntity);
    }

    /**
     * 构造一个将List转化为List对象
     *
     * @param objects 传入的List
     * @return
     */
    private List<ExamEntity> getExamEntity(List<Object> objects) {
        List<ExamEntity> list = new ArrayList<>();
        for (int i = 0; i < objects.size(); i++) {
            Map<String, Object> mp = (Map<String, Object>) objects.get(i);
            ExamEntity entity = new ExamEntity();
            entity.setExamId((Integer) mp.get("examId"));
            entity.setExamAnswer((String) mp.get("examAnswer"));
            list.add(entity);
        }
        return list;
    }

    /**
     * 获取试卷的信息
     */
    @RequestMapping(path = "/getPaper", method = RequestMethod.POST)
    @ResponseBody
    public void getPaper(HttpServletResponse response) {
        String subject = request.getParameter("subject");
        List<ExamPaperEntity> list = examService.getPaper(subject);
        AjaxReturn.informction(new Message(1, "", list), response);
    }

    @PostMapping("/CompleteExamInformation")
    @ResponseBody
    public void CompleteExamInformation(HttpServletResponse response) {
        String examStartTime = request.getParameter("");
        String examTime = request.getParameter("");
        Integer examId = Integer.valueOf(request.getParameter(""));
        ExamPaperEntity entity = new ExamPaperEntity();
        entity.setExamPaperId(examId);
        entity.setExamPaperTime(examTime);
        entity.setExamStartTime(examStartTime);
        AjaxReturn.informction(examService.CompleteExamInformation(entity), response);
    }

    @PostMapping("/DistributeExam")
    @ResponseBody
    public void DistributeExam(HttpServletResponse response) {
        Integer examId = Integer.valueOf(request.getParameter(""));
        Integer studentGrade = Integer.valueOf(request.getParameter(""));
        AjaxReturn.informction(examService.DistributeExam(examId, studentGrade), response);
    }
}
